﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>
#include <QLabel>
#include <QDateTime>
#include <QMessageBox>
#include <QApplication>
#include <QDesktopWidget>
#include "oglwidget.h"
#include "global.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("neuronViewer");
    this->setWindowIcon(QIcon(":/imgs/appIcon.ico"));

    _setScreenHints();

    ui->statusBar->addWidget(new QLabel(QDateTime::currentDateTime().toString()));
    ui->statusBar->setSizeGripEnabled(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::_setScreenHints()
{
    auto welcomeHint = new QLabel();
    welcomeHint->setTextFormat(Qt::RichText);
    QString t = "<font color=gray>";

    t += "<h1><u>Welcome</u></h1>";
    t += "<p></p>";
    t += "<ul>";
    t += "<li>Use menu <b>Window</b> -> <b>Add New</b> to load a default model</li>";
    t += "<li>Use menu <b>File</b> -> <b>Open</b> to load an external model</li>";
    t += "</ul>";

    t += "<h2>Navigation hints</h2>";
    t += "<ul>";
    t += "<li>Use left/right click to rotate/move model</li>";
    t += "<li>Use scroll wheel to zoom model</li>";
    t += "<li>Use keyboard <i>X, Y, Z</i> to snap camera to axis";
    t += "<li>Use keyboard <i>R</i> to reset the view</li>";
    t += "</ul>";

    t += "</font>";
    welcomeHint->setText(t);
    welcomeHint->setMargin(50);
    welcomeHint->setAlignment(Qt::AlignCenter);
    setCentralWidget(welcomeHint);
}

void MainWindow::on_actionOpen_triggered()
{
    QString curPath = "../ObjViewer_UI/";
    QString dlgTitle = tr("Select a file");
    QString filter = tr("3D Models(*.obj)");
    QString fileNameStr = QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);

    if(fileNameStr.isEmpty())
    {
        return;
    }

    if(_screenTips == true){
        _screenTips = false;
        centralWidget()->close();
        takeCentralWidget()->deleteLater();

        setAttribute(Qt::WA_DeleteOnClose,false);
        setCentralWidget(new Window(this,false,true));
    }/*else{
        if(!centralWidget()){//fix bug brings by adding actionClear
            setAttribute(Qt::WA_DeleteOnClose,false);
            setCentralWidget(new Window(this));
        }
    }*/

    emit selectedFile(fileNameStr);
}

void MainWindow::on_actionSave_triggered()
{

}

void MainWindow::on_actionTaubin_triggered()
{
    emit render_Mode(SMOOTH_TAUBIN);
}

void MainWindow::on_actionAddNew_triggered()
{
    if(_screenTips == true){ //current centralWidget is occupied by screenTips
        _screenTips = false;
        centralWidget()->close();
        takeCentralWidget()->deleteLater();
    }

    if(!centralWidget()){
        setAttribute(Qt::WA_DeleteOnClose,false);
        setCentralWidget(new Window(this,true,false));
    }
    else{
        QMessageBox::information(0, tr("Cannot add new window"), tr("Already occupied. Undock first."));
    }
}

void MainWindow::on_actionClear_triggered()
{
    bool hasOther = false;
    if(centralWidget()){
        if(_screenTips == true){
            QString aboutText = tr("<p>Screen hints can't be removed</p>");
            QMessageBox::about(this,tr("Clear tips"),aboutText);
        }else{
            centralWidget()->close();
            takeCentralWidget()->deleteLater();

            hasOther = true;
        }
    }
    if(!centralWidget()||hasOther){
        _screenTips = true;
        _setScreenHints();
    }
}

void MainWindow::on_actionBasic_Info_triggered()
{
    emit render_Mode(BASIC_INFO);
}

void MainWindow::on_actionBackground_triggered()
{
    emit render_Mode(CLEAR_BACKGROUND_COLOR);
}

void MainWindow::on_actionSurface_triggered()
{
    emit render_Mode(PURE_SURFACE_COLOR);
}

void MainWindow::on_actionPoint_triggered()
{
    emit render_Mode(RENDER_POINT);
}

void MainWindow::on_actionWireFrame_triggered()
{
    emit render_Mode(RENDER_WIRE_FRAME);
}

void MainWindow::on_actionFill_triggered()
{
    emit render_Mode(RENDER_FILL);
}

void MainWindow::on_actionFill_Lines_triggered()
{
    emit render_Mode(RENDER_FILL_LINES);
}

void MainWindow::on_actionNone_triggered()
{
    emit render_Mode(LIGHT_DEFAULT);
}

void MainWindow::on_actionDiffuse_triggered()
{
    emit render_Mode(LIGHT_DIFFUSE);
}

void MainWindow::on_actionDirLight_triggered()
{
    emit render_Mode(LIGHT_DIRECTION);
}

void MainWindow::on_actionGourand_triggered()
{
    emit render_Mode(LIGHT_POINT_GOURAND);
}

void MainWindow::on_actionPhong_triggered()
{
    emit render_Mode(LIGHT_POINT_PHONG);
}

void MainWindow::on_actionBlinn_Phong_triggered()
{
    emit render_Mode(LIGHT_POINT_BLINNPHONG);
}

void MainWindow::on_actionNone_2_triggered()
{
    emit render_Mode(MATERIAL_DEFAULT);
}

void MainWindow::on_actionGold_triggered()
{
    emit render_Mode(MATERIAL_GOLD);
}

void MainWindow::on_actionSilver_triggered()
{
    emit render_Mode(MATERIAL_SILVER);
}

void MainWindow::on_actionBronze_triggered()
{
    emit render_Mode(MATERIAL_BRONZE);
}

void MainWindow::on_actionJade_triggered()
{
    emit render_Mode(MATERIAL_JADE);
}

void MainWindow::on_actionPearl_triggered()
{
    emit render_Mode(MATERIAL_PEARL);
}

void MainWindow::on_actionPartColor_triggered()
{
    emit render_Mode(NEURON_DIFFERENT_COLOR);
}

void MainWindow::on_actionGrowSpine_triggered()
{
    emit render_Mode(GROW_SPINE);
}

void MainWindow::on_actionGrowMyelinSheath_triggered()
{
    emit render_Mode(GROW_MYELIN_SHEATH);
}

void MainWindow::on_actionAbout_triggered()
{
    QString aboutText = tr("<h3>Obj viewer 1.0</h3>""<p>Copyright &copy; LLX 2021</p>");
    QMessageBox::about(this,tr("About ObjViewer"),aboutText);
}
